package com.example.springboot.controller;

import com.example.springboot.Exception.ServiceException;
import com.example.springboot.common.OBJ;
import com.example.springboot.common.Result;
import com.example.springboot.service.CommentService;
import com.example.springboot.service.impl.ProductScoreServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：

 * 日期：2024/3/18 20:10
 */
@RestController
@RequestMapping("/comment")
public class CommentController {


    @Autowired
    private CommentService commentService;


    @Autowired
    private ProductScoreServiceImpl productScoreService;

    /**
     * 获取评论列表
     *
     * @return
     */
    @GetMapping("/list")
    public Result getCommentList(@RequestParam("productId") Long productId) {
        List<Comment> list = commentService.getCommentList(productId);
        Long total = commentService.getCommentListTotal(productId);
        Map<String, Object> map = new HashMap<>();
        map.put("commentList", list);
        map.put("total", total);
        return Result.success(map);
    }

    /*    *//**
     * 获取最新评论
     *
     * @return
     *//*
    @GetMapping("/latest")
    public Result getLatestComments() {
        return Result.success(commentService.getLatestComments());
    }*/

    /**
     * 添加评论
     *
     * @param comment
     * @return
     */
    @PostMapping("/add")
    public Result addComment(@RequestBody Comment comment) {
        try {
            // 评论之后用户对该商品的评分为3分
            // 更新评分表，如果没有则插入一条数据，如果有，则判断是否比1分小，如果小更改为1分，如果大不更改
            ProductScore productScore = new ProductScore();
            productScore.setProductId(Long.valueOf(String.valueOf(comment.getProductId())));
            productScore.setUserId(Long.valueOf(String.valueOf(comment.getUserId())));

            // 查询评分表是否有该商品的评分
            ProductScore productScore1 = productScoreService.select(productScore);
            productScore.setScore(3.0f);
            if (productScore1 != null) {
                // 判断是否比1分小
                if (productScore1.getScore() < productScore.getScore()) {
                    productScoreService.update(productScore);
                }
            } else {
                productScoreService.insert(productScore);
            }
        } catch (Exception e) {
            throw new ServiceException("");
        }

        if (commentService.save(comment)) {
            return Result.success("评论成功");
        }
        return Result.error("评论失败");
    }

    /**
     * 删除评论
     *
     * @param comm
     * @return
     */
    @PostMapping("/delete")
    public Result deleteComment(@RequestBody Comment comm) {
        if (commentService.removeComment(comm))
            return Result.success("删除评论成功");
        return Result.error("删除评论失败");
    }


    @PostMapping("/selectInPage")
    public Result selectInPage(@RequestBody OBJ obj) {
        return Result.success(commentService.selectInPage(obj.getComment(), obj.getCurrentPage(), obj.getPageSize()));
    }

    @PostMapping("/deleteList")
    public Result deleteList(@RequestBody List<Comment> comments) {
        if (commentService.removeComments(comments))
            return Result.success("删除评论成功");
        return Result.error("删除评论失败");
    }
}
